package org.train.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.jackson.JsonObjectSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;
import java.text.SimpleDateFormat;

@Configuration
public class JacksonConfig{
    @Bean
    public ObjectMapper objectMapper(){
        ObjectMapper objectMapper = new ObjectMapper();
        objectMapper.setDateFormat(new SimpleDateFormat("yyyy-MM-dd"));
        /*
        // TODO 将值为null改写成""
        objectMapper.getSerializerProvider().setNullValueSerializer(new JsonObjectSerializer<Object>(){
            @Override
            protected void serializeObject(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException{
                jgen.writeString("");
            }
        });
        */
        return objectMapper;
    }
}
